import { Injectable } from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { FindOneOptions, In, Repository } from 'typeorm'
import { PAGENATION } from '@/constants/pagenation.constants'
import { Menu } from '@/entities/menu.entity'
import { Permission } from '@/entities/permission.entity'
import { Role } from '@/entities/role.entity'
import { CreateRoleDto } from './dto/create-role.dto'
import { UpdateRoleDto } from './dto/update-role.dto'

@Injectable()
export class RolesService {
  constructor(
    @InjectRepository(Role)
    private readonly roleRepository: Repository<Role>,
    @InjectRepository(Menu)
    private readonly menuRepository: Repository<Menu>,
    @InjectRepository(Permission)
    private readonly permissionRepository: Repository<Permission>
  ) {}
  create(createRoleDto: CreateRoleDto) {
    return this.roleRepository.save(createRoleDto)
  }

  async findAllAndCount(query, options: FindOneOptions<Role> = null) {
    const { current = PAGENATION.current, size = PAGENATION.size, ...rest } = query
    const take = size // 条数
    const skip = (current - 1) * take // 页码 - 要跳过多少条
    const params: FindOneOptions<Role> = {
      select: [
        'id',
        'roleCode',
        'roleName',
        'roleDesc',
        'createBy',
        'createId',
        'createTime',
        'updateBy',
        'updateId',
        'updateTime',
        'status',
      ],
      where: rest,
    }
    if (options) {
      for (const key in options) {
        params[key] = options[key]
      }
    }
    const [records, total] = await this.roleRepository.findAndCount({
      ...params,
      take,
      skip,
    })
    return {
      records,
      total,
      current,
      size,
    }
  }

  async findAll(query, options: FindOneOptions<Role> = null) {
    const params: FindOneOptions<Role> = {
      select: [
        'id',
        'roleCode',
        'roleName',
        'roleDesc',
        'createBy',
        'createId',
        'createTime',
        'updateBy',
        'updateId',
        'updateTime',
        'status',
      ],
      where: query,
    }
    if (options) {
      for (const key in options) {
        params[key] = options[key]
      }
    }
    return this.roleRepository.find({
      ...params,
    })
  }

  findOne(id: string, options: FindOneOptions<Role> = null) {
    const params: FindOneOptions<Role> = {
      where: { id },
    }
    if (options) {
      for (const key in options) {
        params[key] = options[key]
      }
    }
    return this.roleRepository.findOne(params)
  }

  async update(id: string, updateRoleDto: UpdateRoleDto, operatorUser?: PayloadUserType) {
    console.log('operatorUser', operatorUser)
    const role = await this.findOne(id)
    const { menuIds, buttons, ...rest } = updateRoleDto
    const params: Role = {
      ...role,
      ...rest,
    }
    if (menuIds) {
      params.menus = []
      if (menuIds.length) {
        const menus = await this.menuRepository.findBy({
          id: In(menuIds),
        })
        if (menus.length) {
          params.menus = menus
        }
      }
    }
    if (buttons) {
      params.permissions = []
      if (buttons.length) {
        const buttonArr = await this.permissionRepository.findBy({
          id: In(buttons),
        })
        if (buttonArr.length) {
          params.permissions = buttonArr
        }
      }
    }
    return this.roleRepository.save(this.roleRepository.create(params))
  }

  remove(ids: string[]) {
    this.roleRepository.delete(ids)
    return `删除成功`
  }
}
